﻿using Kendo.Mvc.UI;
using MoviesRoom.Areas.Administration.ViewModels;
using MoviesRoom.Controllers;
using MoviesRoom.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kendo.Mvc.Extensions;

namespace MoviesRoom.Areas.Administration.Controllers
{
    [Authorize(Roles = "admin")]
    public class AdminVotesController : BaseController
    {
        public AdminVotesController() : base(new UowData())
        {
        }

        public AdminVotesController(IUowData data) : base(data)
        {
        }

        //
        // GET: /Administration/Votes/
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult ReadVotes([DataSourceRequest]
                                    DataSourceRequest request)
        {
            var votes = this.Data.Votes.All().Select(VotesViewModel.FromVotes);
            return Json(votes.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }

        public JsonResult DeleteVotes([DataSourceRequest]
                                      DataSourceRequest request, VotesViewModel vote)
        {
            if (vote != null && ModelState.IsValid)
            {
                this.Data.Votes.Delete(vote.Id);
                this.Data.SaveChanges();
            }

            return Json(new[] { vote }.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
    }
}